package com.example.blog.controller.admin;

import com.example.blog.pojo.Blog;
import com.example.blog.pojo.Type;
import com.example.blog.pojo.User;
import com.example.blog.pojo.query.SearchBlog;
import com.example.blog.service.BlogService;
import com.example.blog.service.TypeService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;

/**
 * name: 东北宋小宝
 * date: 2020/8/17
 */
@Controller
@RequestMapping("/admin")
public class BlogController {
    @Resource
    private BlogService blogService;
    @Resource
    private TypeService typeService;
    @Value("${blogpage.blog}")
    private int pageNum;
    /**
     * 博客首页展示
     * @param page 当前页
     * @param model
     * @return
     */
    @GetMapping("/toBlogs")
    public String toBlogs(Integer page, Model model){
        PageInfo pageInfo = blogService.getPage(page, pageNum);
        List<Type> types = typeService.findAll();
        model.addAttribute("types",types);
        model.addAttribute("pageInfo", pageInfo);
        return "/admin/blogs";
    }

    /**
     * 查询博客
     * @param searchBlog 查询条件
     * @param model
     * @param page  查询页
     * @return
     */
    @PostMapping("/blogs/search")
    public String query(SearchBlog searchBlog, Model model,
                        @RequestParam(defaultValue = "1",value = "page") Integer page){
        PageInfo pageInfo = blogService.queryPage(page, pageNum,searchBlog);
        model.addAttribute("pageInfo",pageInfo);
        return "/admin/blogs::blogList";
    }

    /**
     * 前往编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/blogs/edit")
    public String toEdit(Integer id, Model model){
        Blog blog = blogService.getBlog(id);
        List<Type> all = typeService.findAll();
        model.addAttribute("blog",blog);
        model.addAttribute("types",all);
        return "/admin/blogs-input";
    }

    /**
     * 保存博客
     * @param blog 博客内容信息类
     * @return
     */
    @PostMapping("/blogs/save")
    public String save(Blog blog, RedirectAttributes attributes){
        String message = "保存成功";
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        blog.setUser(user);
        blog.setUserId(user.getId());
        System.out.println(blog);
        try {
            blogService.saveBlog(blog);
        } catch (Exception e) {
            message = "保存失败";
        }
        attributes.addFlashAttribute("message",message);
        return "redirect:/admin/toBlogs";
    }

    /**
     * 删除博客
     * @param id
     * @return
     */
    @GetMapping("/blogs/del")
    public String del(Integer id, RedirectAttributes attributes){
        String message = "删除成功";
        try {
            blogService.delete(id);
        } catch (Exception e) {
            message = "删除失败";
        }
        attributes.addFlashAttribute("message",message);
        return "redirect:/admin/toBlogs";
    }
}
